package org.desktop.web.client.project.form;

import org.desktop.web.client.custom.widget.AbstractWindow;
import org.desktop.web.client.project.grid.ProjectGrid;
import org.desktop.web.client.project.service.ProjectService;
import org.desktop.web.client.project.service.ProjectServiceAsync;
import org.desktop.web.shared.domain.entities.Project;

import com.google.gwt.core.shared.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.IsWidget;
import com.sencha.gxt.core.client.util.Margins;
import com.sencha.gxt.widget.core.client.FramedPanel;
import com.sencha.gxt.widget.core.client.box.AlertMessageBox;
import com.sencha.gxt.widget.core.client.box.MessageBox;
import com.sencha.gxt.widget.core.client.button.ButtonBar;
import com.sencha.gxt.widget.core.client.button.TextButton;
import com.sencha.gxt.widget.core.client.container.BoxLayoutContainer.BoxLayoutData;
import com.sencha.gxt.widget.core.client.container.BoxLayoutContainer.BoxLayoutPack;
import com.sencha.gxt.widget.core.client.container.MarginData;
import com.sencha.gxt.widget.core.client.container.VerticalLayoutContainer;
import com.sencha.gxt.widget.core.client.container.VerticalLayoutContainer.VerticalLayoutData;
import com.sencha.gxt.widget.core.client.event.SelectEvent;
import com.sencha.gxt.widget.core.client.event.SelectEvent.SelectHandler;
import com.sencha.gxt.widget.core.client.form.FieldLabel;
import com.sencha.gxt.widget.core.client.form.TextField;

public class ProjectFormWindow extends AbstractWindow implements IsWidget {


	private FramedPanel mainPanel;

	private TextField projectName;
	
	private ProjectGrid toUpdate;


	public ProjectFormWindow(ProjectGrid toUpdate) {
		super();
		this.toUpdate=toUpdate;
		setModal(true);
		mainPanel = new FramedPanel();
		mainPanel.setHeaderVisible(false);
		setHeaderVisible(true);
		setHeadingText("Add project");



	}

	private void addSaveEvent(TextButton save,final ProjectGrid grid) {
		save.addSelectHandler(new SelectHandler() {
			
			@Override
			public void onSelect(SelectEvent event) {
				Project project = new Project();
				project.setDescProject(projectName.getValue());
				ProjectServiceAsync service = GWT.create(ProjectService.class);
				service.saveProject(project,new AsyncCallback<Boolean>() {

					@Override
					public void onFailure(Throwable caught) {
					
						AlertMessageBox amb = new AlertMessageBox("Error", caught.getMessage());
						amb.setIcon(MessageBox.ICONS.error());
						amb.show();
					}

					@Override
					public void onSuccess(Boolean result) {
						// TODO Auto-generated method stub
						if(result){
							grid.load();
							hide();
							
						}
						
					}
				});
			}
		});
	}

	private void addCancelEvent(TextButton cancel) {
		cancel.addSelectHandler(new SelectHandler() {
			
			@Override
			public void onSelect(SelectEvent event) {
				hide();
				
			}
		});
	}

	@Override
	public String id() {
		// TODO Auto-generated method stub
		return "PROJECT_FORM";
	}

	@Override
	public void init() {
		VerticalLayoutContainer fieldsContainer = new VerticalLayoutContainer();

		projectName = new TextField();
		projectName.setAllowBlank(false);
		fieldsContainer.add(new FieldLabel(projectName, "Nome progetto"),
				new VerticalLayoutData(1, -1));


		ButtonBar buttonBar = new ButtonBar();
		TextButton save = new TextButton("Save");
		TextButton cancel = new TextButton("Cancel");
		buttonBar.add(save, new BoxLayoutData(new Margins(10)));
		buttonBar.add(cancel, new BoxLayoutData(new Margins(10)));
		
		setButtonAlign(BoxLayoutPack.CENTER);
		addButton(buttonBar);
		
		addCancelEvent(cancel);
		addSaveEvent(save,toUpdate);
		
		mainPanel.add(fieldsContainer, new MarginData(10));
		
		
		
		add(mainPanel);		
	}

}
